// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: wsfuyibing <682805@qq.com>
// Date: 2024-07-24

// Package src
// iris web framework bootstrap.
package src

import (
	"gitee.com/go-libs/console"
	"gitee.com/go-libs/db-xorm/db/commands/gen_model"
	"gitee.com/go-libs/db-xorm/db/commands/gen_service"
	"gitee.com/go-wares/framework-iris/framework/src/commands/gen_annotation"
	"gitee.com/go-wares/framework-iris/framework/src/commands/starter"
	"gitee.com/go-wares/framework-iris/framework/src/commands/stopper"
)

// Component
// is a top level component for iris web framework with console.
type Component struct {
	Application *Application
	Console     *console.Container
}

// New
// creates a top level component instance with a console and an application.
func New() *Component {
	return (&Component{
		Application: (&Application{}).init(),
		Console:     console.NewContainer(),
	}).init()
}

// Run
// runs the top level component. It's schedule to console runner.
func (o *Component) Run() {
	o.Console.Run()
}

// +---------------------------------------------------------------------------+
// | Access methods                                                            |
// +---------------------------------------------------------------------------+

// Init
// component fields.
func (o *Component) init() *Component {
	// Add
	// builtin and lib commands to console container.
	o.Console.Add(
		starter.New(o.Application.start), // go run main.go start
		stopper.New(),                    // go run main.go stop
		gen_annotation.New(),             // go run main.go gen:annotation
		gen_model.New(),                  // go run main.go gen:model
		gen_service.New(),                // go run main.go gen:service
	)
	return o
}
